<!--
 * @Description:
 * @Author: william
 * @Date: 2025-02-17 16:47:40
 * @LastEditors: william
 * @LastEditTime: 2025-05-13 13:59:54
-->
<template>
  <BaseDialog
    v-model="dialogVisible"
    :title="title"
    style="width: 550px"
    width="550px"
    :before-close="handleClose"
  >
    <el-form
      ref="refDialogForm"
      v-loading="formLoading"
      class="set-dialog-form"
      :label-width="language === 'en' ? '180px' : '100px'"
      :model="formData"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('fleet.expenseItem')" prop="name">
            <TextInput
              v-model="formData.name"
              clearable
              :label="$t('fleet.expenseItem')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('hotel.amount')" prop="amount">
            <template #label>
              <label>{{ $t('hotel.amount') }}</label>
              <el-tooltip
                :content="$t('fleet.onlyInputIntOrDec')"
                placement="top"
                effect="light"
              >
                <el-icon><i-ep-QuestionFilled /></el-icon>
              </el-tooltip>
            </template>
            <TextInput
              v-model="formData.amount"
              :label="$t('hotel.amount')"
              verify-type="float"
              @blur="handleInputBlur('amount')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            :label="`${$t('public.remark')}（${$t('fleet.optional')}）`"
            prop="note"
          >
            <TextInput
              v-model="formData.note"
              type="textarea"
              :rows="2"
              :label="$t('public.remark')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="form-dialog-footer">
        <el-button plain @click="handleClose">{{
          $t('public.cancel')
        }}</el-button>
        <el-button type="primary" @click="handleSubmitForm">{{
          $t('public.confirm')
        }}</el-button>
      </div>
    </template>
  </BaseDialog>
</template>

<script setup>
const props = defineProps({
  type: {
    type: String,
    default: 'add' // add | edit
  },
  orderId: {
    type: String,
    default: ''
  },
  row: {
    type: Object,
    default: () => {}
  }
})

const emit = defineEmits(['update'])
const dialogVisible = defineModel({
  type: Boolean,
  default: false
})
const { language } = storeToRefs(useAppStore())
const title = computed(() =>
  props.type === 'add' ? g_t('public.create') : g_t('public.edit')
)

const refDialogForm = ref(null)
const formLoading = ref(false)
const formData = ref({
  name: '',
  amount: '',
  note: ''
})
const rules = {
  name: g_rules.commonInputText(g_t('fleet.expenseItem')),
  amount: g_rules.amount2
}

onMounted(() => {
  props.type === 'edit' && getFormData()
})

const getFormData = () => {
  g_utils.assignFormData(formData.value, props.row)
  formData.value.id = props.row.id
  console.log('formData.value', formData.value)
}

const handleClose = () => {
  dialogVisible.value = false
}

const handleInputBlur = prop => {
  // 数字化处理
  refDialogForm.value.validateField([prop], vaild => {
    if (vaild && formData.value[prop]) {
      formData.value[prop] = formData.value[prop] * 1
    }
  })
}

const handleSubmitForm = () => {
  if (formLoading.value) return
  refDialogForm.value.validate(valid => {
    console.log('formData.value', formData.value)
    if (!valid) return
    formLoading.value = true
    const urlMethod =
      props.type === 'add'
        ? g_api.fleet.apiExpenseCreate
        : g_api.fleet.apiExpenseUpdate
    urlMethod({
      ...formData.value,
      orderId: props.orderId
    })
      .then(res => {
        if (res.code === '200') {
          ElMessage({
            message: res.message,
            type: 'success'
          })
          handleClose()
          emit('update')
        }
      })
      .finally(() => {
        formLoading.value = false
      })
  })
}
</script>

<style scoped lang="scss"></style>
